package pers.xx.sbm.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import pers.xx.sbm.entity.BaseDict;
import pers.xx.sbm.entity.User;
import pers.xx.sbm.service.DictService;
import pers.xx.sbm.utils.UserUtils;
import pers.xx.sbm.vo.ResResult;

import java.util.Date;

/**
 * @author ：xiexing
 * @description：字典Controller
 * @date ：2020/8/17 14:18
 */
@Controller
@RequestMapping("/dict")
public class DictController {

    @Autowired
    UserUtils userUtils;//用户工具

    @Autowired
    private DictService dictService;

    /**
     * @description 字典管理
     * @author XieXing
     * @date 2020/8/11 15:45
     */
    @RequestMapping("/")
    public String dictPage(Model model) {
        return "page/system/dict";
    }

    /**
     * @description 获取字典列表
     * @author XieXing
     * @date 2020/8/16 12:02
     */
    @RequestMapping("/getList")
    @ResponseBody
    public ResResult getDictList(BaseDict dict, Integer page, Integer limit, String searchContent) {
        if (!StringUtils.isEmpty(searchContent)) {
            dict.setDictName(searchContent);
        }
        ResResult resResult = dictService.listByPage(dict, page, limit);
        resResult.setOptName("获取字典列表");
        return resResult;
    }

    /**
     * @description 保存字典信息
     * @author XieXing
     * @date 2020/8/17 14:22
     */
    @RequestMapping("/save")
    @ResponseBody
    public ResResult saveDict(BaseDict dict) {
        User currentUser = userUtils.getCurrentUser();//获取当前用户
        dict.setDictAddby(currentUser.getUserName());
        dict.setDictAddTime(new Date());
        dict.setDictAddbyId(currentUser.getId());
        ResResult resResult = dictService.insertSelective(dict);
        resResult.setOptName("保存字典信息");
        return resResult;
    }


    /**
     * @description 更新字典信息
     * @author XieXing
     * @date 2020/8/17 22:03
     */
    @RequestMapping("/update")
    @ResponseBody
    public ResResult updateDict(BaseDict dict) {
        User currentUser = userUtils.getCurrentUser();//获取当前用户
        dict.setDictUpdateby(currentUser.getUserName());
        dict.setDictUpdateTime(new Date());
        dict.setDictUpdatebyId(currentUser.getId());
        ResResult resResult = dictService.updateByPrimaryKeySelective(dict);
        resResult.setOptName("更新字典信息");
        return resResult;
    }

    /**
     * @description 删除字典 逻辑删除
     * @author XieXing
     * @date 2020/8/17 23:02
     */
    @RequestMapping("/{id}")
    @ResponseBody
    public ResResult delDict(@PathVariable(value = "id") Integer id) {
        ResResult resResult = dictService.deleteByPrimaryKey(id);
        resResult.setOptName("删除字典信息");
        return resResult;
    }


    /**
     * @description 更新字典状态
     * @author XieXing
     * @date 2020/8/23 21:24
     */
    @RequestMapping("/{id}/{status}")
    @ResponseBody
    public ResResult updateDictStatus(@PathVariable(value = "id") Integer id, @PathVariable(value = "status") Integer status) {
        User currentUser = userUtils.getCurrentUser();
        BaseDict dict = new BaseDict();
        dict.setId(id);
        dict.setDictStatus(status);
        dict.setDictUpdateby(currentUser.getUserName());
        dict.setDictUpdatebyId(currentUser.getId());
        dict.setDictUpdateTime(new Date());
        ResResult resResult = dictService.updateByPrimaryKeySelective(dict);
        resResult.setOptName("更新字典状态");
        return resResult;
    }


    /**
     * @description 获取字典信息
     * @author XieXing
     * @date 2020/10/10 9:33
     */
    @RequestMapping(value = "/getDict", method = RequestMethod.POST)
    @ResponseBody
    public ResResult getDict() {
        ResResult resResult = dictService.selectAll();
        resResult.setOptName("获取字典信息");
        return resResult;
    }

}
